import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },

      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  {
    path: "/goods",
    name: "goods",
    meta: {
      title: "商品管理",
      icon: "system",
      hidden: false,
      alwaysShow: false,
      params: null,
    },
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/goods/index.vue"),
        name: "GoodsIndex",
        meta: {
          title: "商品管理",
          icon: "el-icon-Monitor",
          hidden: false,
          keepAlive: false,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "add-goods",
        component: () => import("@/views/goods/component/addGoods.vue"),
        name: "AddGoods",
        meta: {
          title: "添加商品",
          hidden: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "add-goods-image",
        component: () => import("@/views/goods/component/addGoodsImage.vue"),
        name: "AddGoodsImage",
        meta: {
          title: "添加商品图片",
          hidden: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },

      {
        path: "store",
        component: () => import("@/views/goods/store.vue"),
        name: "Store",
        meta: {
          title: "店铺管理",
          icon: "el-icon-Wallet",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "category",
        component: () => import("@/views/goods/category.vue"),
        name: "Category",
        meta: {
          title: "分类管理",
          icon: "el-icon-Wallet",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
    ],
  },

  {
    path: "/order",
    name: "Order",
    meta: {
      title: "订单管理",
      icon: "el-icon-Tickets",
      hidden: false,
      alwaysShow: false,
      params: null,
    },
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/order/index.vue"),
        name: "OrderIndex",
        meta: {
          title: "订单管理",
          icon: "el-icon-Tickets",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "promotion",
        component: () => import("@/views/order/promotion.vue"),
        name: "Promotion",
        meta: {
          title: "促销列表",
          icon: "el-icon-Tickets",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "address",
        component: () => import("@/views/order/address.vue"),
        name: "Address",
        meta: {
          title: "地址列表",
          icon: "el-icon-Location",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
    ],
  },

  {
    path: "/system",
    redirect: "/system/user",
    name: "/system",
    meta: {
      title: "系统管理",
      icon: "el-icon-Setting",
      hidden: false,
      alwaysShow: false,
      params: null,
    },
    component: Layout,
    children: [
      {
        path: "user",
        component: () => import("@/views/system/user/index.vue"),
        name: "User",
        meta: {
          title: "用户管理",
          icon: "el-icon-User",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "role",
        component: () => import("@/views/system/role/index.vue"),
        // component: "system/role/index",
        name: "Role",
        meta: {
          title: "角色管理",
          icon: "role",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "menu",
        component: () => import("@/views/system/menu/index.vue"),
        // component: "system/menu/index",
        name: "SysMenu",
        meta: {
          title: "菜单管理",
          icon: "menu",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
